<template>
  <div class="home-container">
    <!-- 管理员操作区域 -->
    <div class="function-buttons" v-if="isAdmin">
      <h1>仅供管理员操作：</h1>
      <router-link to="/add-book" class="function-button">增加书籍</router-link>
      <router-link to="/delete-book" class="function-button">删除书籍</router-link>
      <!-- 其他操作按钮 -->

      <!-- 刷新按钮 -->
      <button @click="refreshData" class="refresh-button">
        <i class="fas fa-sync-alt"></i> 刷新数据
      </button>

      <!-- 数据展示区 -->
      <div class="data-grid">
        
        <!-- 借阅信息区块 -->
        <div class="data-card borrow-section">
          <h2 class="data-title">
            <i class="fas fa-book-open"></i> 借阅记录
            <span class="badge">{{ borrowData.length }}</span>
          </h2>
          <div class="scroll-container">
            <div v-if="borrowData.length > 0" class="data-content">
              <div class="data-row header">
                <div class="col">记录ID</div>
                <div class="col">图书ID</div>
                <div class="col">用户ID</div>
                <div class="col">借阅日期</div>
                <div class="col">到期日期</div>
                <div class="col">状态</div>
                <div class="col">操作员</div>
                <div class="col">图书名称</div>
              </div>
              <div v-for="item in borrowData" 
                   :key="item.id" 
                   class="data-row">
                <div class="col">{{ item.id }}</div>
                <div class="col">{{ item.book_id }}</div>
                <div class="col">{{ item.user_id }}</div>
                <div class="col">{{ formatDate(item.borrow_date) }}</div>
                <div class="col">{{ formatDate(item.due_date) }}</div>
                <div class="col status-tag" :class="item.status">{{ item.status }}</div>
                <div class="col">{{ item.operator }}</div>
                <div class="col">{{ item.book_name }}</div>
              </div>
            </div>
            <p v-else class="no-data">暂无借阅记录</p>
          </div>
        </div>

        <!-- 归还日志区块 -->
        <div class="data-card return-section">
          <h2 class="data-title">
            <i class="fas fa-undo-alt"></i> 归还日志
            <span class="badge">{{ returnData.length }}</span>
          </h2>
          <div class="scroll-container">
            <div v-if="returnData.length > 0" class="data-content">
              <div class="data-row header">
                <div class="col">记录ID</div>
                <div class="col">操作员</div>
                <div class="col">用户ID</div>
              </div>
              <div v-for="item in returnData" 
                   :key="item.borrow_id" 
                   class="data-row">
                <div class="col">{{ item.borrow_id }}</div>
                <div class="col">{{ item.operator }}</div>
                <div class="col">{{ item.user_id }}</div>
              </div>
            </div>
            <p v-else class="no-data">暂无归还日志</p>
          </div>
        </div>

        <!-- 操作日志区块 -->
        <div class="data-card log-section">
          <h2 class="data-title">
            <i class="fas fa-clipboard-list"></i> 操作日志
            <span class="badge">{{ logData.length }}</span>
          </h2>
          <div class="scroll-container">
            <div v-if="logData.length > 0" class="data-content">
              <div class="data-row header">
                <div class="col">日志ID</div>
                <div class="col">操作类型</div>
                <div class="col">数量</div>
                <div class="col">时间</div>
              </div>
              <div v-for="item in logData" 
                   :key="item.log_id" 
                   class="data-row">
                <div class="col">{{ item.log_id }}</div>
                <div class="col">{{ item.change_type }}</div>
                <div class="col">{{ item.change_amount }}</div>
                <div class="col">{{ formatDate(item.created_at) }}</div>
              </div>
            </div>
            <p v-else class="no-data">暂无操作日志</p>
          </div>
        </div>
      </div>
    </div>

    <div v-else>
      <header class="system-header">
        <h1>欢迎使用威龙图书借阅系统</h1>
        <p>让阅读触手可及，让知识自由流动</p>
      </header>

      <!-- 借阅流程指引 -->
      <section class="process-section">
        <h2 class="section-title">📚 图书借阅三步走</h2>
        <div class="step-cards">
          <div class="step-card">
            <div class="step-icon">1</div>
            <h3>查找图书</h3>
            <p>通过检索系统或分类导航找到心仪图书</p>
          </div>
          <div class="step-card">
            <div class="step-icon">2</div>
            <h3>自助借阅</h3>
            <p>在威龙数字图书上完成借阅</p>
          </div>
          <div class="step-card">
            <div class="step-icon">3</div>
            <h3>按时归还</h3>
            <p>请在借阅期内归还至任意校区图书馆</p>
          </div>
        </div>
      </section>

      <!-- 借阅须知 -->
      <section class="notice-section">
        <h2 class="section-title">❗ 借阅须知</h2>
        <div class="notice-list">
          <div class="notice-item">
            <div class="notice-icon">📅</div>
            <div>
              <h3>借阅期限</h3>
              <p>30天</p>
            </div>
          </div>
          <div class="notice-item">
            <div class="notice-icon">⏰</div>
            <div>
              <h3>续借规则</h3>
              <p>可续借1次（相同时长），逾期不可续借</p>
            </div>
          </div>
          <div class="notice-item">
            <div class="notice-icon">💸</div>
            <div>
              <h3>逾期费用</h3>
              <p>0.2元/天/册，最高不超过图书原价</p>
            </div>
          </div>
          <div class="notice-item">
            <div class="notice-icon">📖</div>
            <div>
              <h3>损坏赔偿</h3>
              <p>轻微损坏修补，严重损坏按原价1-3倍赔偿</p>
            </div>
          </div>
        </div>
      </section>
    </div>
  </div>
</template>

<script>
import axios from 'axios';

export default {
  name: 'LibraryHome',
  data() {
    return {
      isAdmin: false,
      borrowData: [],
      returnData: [],
      logData: [],
      isLoading: false,
    };
  },
  mounted() {
    this.checkAdminStatus();
    this.fetchData();
  },
  methods: {
    checkAdminStatus() {
      try {
        const userData = localStorage.getItem('user');
        if (!userData) {
          return;
        }
        const parsedUserData = JSON.parse(userData);
        if (parsedUserData.isAdmin === 1) {
          this.isAdmin = true;
        }
      } catch (error) {
        console.error('检查管理员状态时出错:', error);
      }
    },
    async fetchData() {
      if (!this.isAdmin) {
        return;
      }

      this.isLoading = true;

      try {
        // 获取借阅信息
        const borrowResponse = await axios.get('http://8.134.212.44:9999/admin/borrow');
        this.borrowData = borrowResponse.data.data;

        // 获取归还日志
        const returnResponse = await axios.get('http://8.134.212.44:9999/admin/return');
        this.returnData = returnResponse.data.data;

        // 获取操作日志
        const logResponse = await axios.get('http://8.134.212.44:9999/admin/log');
        this.logData = logResponse.data.data;
      } catch (error) {
        console.error('获取数据失败:', error);
        alert('获取数据失败，请稍后重试');
      } finally {
        this.isLoading = false;
      }
    },
    formatDate(dateString) {
      const date = new Date(dateString);
      return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
    },
    refreshData() {
      this.fetchData(); // 调用 fetchData 方法重新获取数据
    }
  }
};
</script>

<style scoped>

.refresh-button {
  margin-top: 20px;
  padding: 10px 15px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.refresh-button:hover {
  background-color: #45a049;
}
.print-area {
  margin: 20px 0;
  padding: 15px;
  background-color: #f5f5f5;
  border-radius: 5px;
}
/* 测试数据展示区域样式 */
/* .data-container {
  margin: 20px 0;
  padding: 15px;
  background-color: #f8f9fa;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.data-block {
  margin-top: 10px;
}

.data-block h4 {
  margin-top: 0;
  margin-bottom: 10px;
  color: #2c3e50;
}

.storage-details {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.data-item {
  display: flex;
  gap: 8px;
  padding: 8px;
  background-color: white;
  border-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.data-label {
  font-weight: bold;
  min-width: 80px;
}

.data-value {
  flex: 1;
} */

.no-data {
  padding: 15px;
  background-color: #fff3cd;
  color: #856404;
  border-radius: 4px;
  text-align: center;
}
.home-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

.system-header {
  text-align: center;
  margin-bottom: 3rem;
  padding: 2rem;
  background: linear-gradient(135deg, #42b983 0%, #358f6c 100%);
  border-radius: 12px;
  color: white;
}

.system-header h1 {
  font-size: 2.5rem;
  margin-bottom: 1rem;
}

.section-title {
  font-size: 1.8rem;
  color: #2c3e50;
  margin-bottom: 2rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

/* 流程步骤样式 */
.step-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 2rem;
  margin-bottom: 4rem;
}

.step-card {
  background: white;
  padding: 2rem;
  border-radius: 12px;
  box-shadow: 0 4px 6px rgba(0,0,0,0.1);
  transition: transform 0.3s ease;
  text-align: center;
}

.step-card:hover {
  transform: translateY(-5px);
}

.step-icon {
  width: 50px;
  height: 50px;
  background: #42b983;
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  margin: 0 auto 1rem;
}
.function-buttons {
  display: flex;
  align-items: center; /* 垂直居中对齐 */
  gap: 0.5rem; /* 按钮之间的间距 */
  padding: 0; /* 移除外边距 */
  margin: 1rem; /* 给按钮区域添加外边距 */
}

.function-button {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem 1rem; /* 内边距 */
  background: #42b983; /* 按钮背景色 */
  color: #fff; /* 文字颜色 */
  border: none; /* 无边框 */
  border-radius: 8px; /* 圆角 */
  font-size: 16px; /* 字体大小 */
  text-decoration: none; /* 移除链接下划线 */
  transition: background 0.3s ease, transform 0.3s ease; /* 过渡效果 */
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); /* 阴影 */
}

.function-button:hover {
  transform: translateY(-2px); /* 轻微上移 */
}
/* 须知列表样式 */
.notice-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 2rem;
}

.notice-item {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  padding: 1.5rem;
  background: #f8f9fa;
  border-radius: 8px;
  transition: all 0.3s ease;
}

.notice-item:hover {
  background: white;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.notice-icon {
  font-size: 2rem;
  flex-shrink: 0;
}

@media (max-width: 768px) {
  .system-header h1 {
    font-size: 2rem;
  }

  .step-cards,
  .notice-list {
    grid-template-columns: 1fr;
  }

  .notice-item {
    flex-direction: column;
    text-align: center;
  }
  
}
</style>